package {


import components.AreaItem;

import core.Facade;

import events.AreaSelectedEvent;
import events.DigLevelEvent;

import flash.display.LoaderInfo;


import flash.display.Sprite;
import flash.display.Stage;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.system.Security;

import ui.ShortMessagePopup;

import utils.TraceObject;


[SWF(width="668", height="668", backgroundColor="#000000", frameRate="30")]
public class VictoryDay extends Sprite {
    private var _mapLevel:MapLevel;
    private var _diglevel:DigLevel;

    public function VictoryDay() {
        this.stage.scaleMode = StageScaleMode.NO_SCALE;
         Security.loadPolicyFile("http://81.177.160.137/crossdomain.xml");



//INIT VARIABLES
        var myFlashVar:String;
        addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(event:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        Facade.getInstance().stage = this.stage;
          var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
        if (paramObj){
            if (paramObj.auth_key){
                Facade.getInstance().auth_key =paramObj.auth_key;
            } else{
                 Facade.getInstance().auth_key = "0ccc8afc8341dd5b65922d4c5906aeb2";
            }
            if (paramObj.id_vkontakte){
                Facade.getInstance().id_vkontakte =paramObj.id_vkontakte;
            } else {
              Facade.getInstance().id_vkontakte = "3585394";
            }

        }


        _mapLevel = new MapLevel();
        _mapLevel.create();

        _mapLevel.activate();
        addChild(_mapLevel);
        addEventListener(AreaSelectedEvent.AREA_PLAY, onPlay);
    }

    private function onPlay(event:AreaSelectedEvent):void {
        var area:AreaItem = event.target as AreaItem;
        _mapLevel.deactivate();
        _diglevel = new DigLevel(area);

        _diglevel.fillFromArea(area);
        addChild(_diglevel);
        _diglevel.create();
        _diglevel.activate();

        addEventListener(DigLevelEvent.EXIT_LEVEL, onDigExit);

    }

    private function onDigExit(event:DigLevelEvent):void {
        ShortMessagePopup.getInstance().hideMessage();
        _diglevel.deactivate();
        removeChild(_diglevel);
        _diglevel.destroy();

        _diglevel = null;


        _mapLevel.activate();
        addChild(_mapLevel);
    }

}

}